﻿namespace D.Wx.Dtos
{
    /// <summary>
    /// 返回结果
    /// </summary>
    public static class ResponseUtil
    {
        /// <summary>
        /// 增加、删除、批量删除、修改，返回操作结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public static ApiResult<string> NormalResponse(this int result)
        {
            if (result > 0)
            {
                return new ApiResult<string>();
            }
            return new ApiResult<string>(StatusCodeEnum.Error, false, "操作失败");
        }

        /// <summary>
        /// 返回用户提醒信息
        /// </summary>
        /// <param name="message">提醒内容</param>
        /// <returns></returns>
        public static ApiResult<string> WaringResponse(this string message)
        {
            return new ApiResult<string>(StatusCodeEnum.Waring, false, message);
        }
    }

    /// <summary>
    /// 返回参数
    /// </summary>
    /// <typeparam name="T">需要返回的实体</typeparam>
    public class ApiResult<T> where T : class
    {
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="_statusCode"></param>
        /// <param name="_success"></param>
        /// <param name="_msg"></param>
        public ApiResult(StatusCodeEnum _statusCode = StatusCodeEnum.OK, bool _success = true, string _msg = null)
        {
            StatusCode = _statusCode;
            Success = _success;

            if (_msg == null)
            {
                if (StatusCode == StatusCodeEnum.OK)
                {
                    Msg = StatusCode.ToDescriptionOrString();
                }
                else if (StatusCode == StatusCodeEnum.Error)
                {
                    Msg = StatusCode.ToDescriptionOrString();
                }
                else if (_statusCode == StatusCodeEnum.Waring)
                {
                    Msg = _msg;
                }
                else
                {
                    Msg = StatusCode.ToDescriptionOrString();
                }
            }
            else
            {
                Msg = _msg;
            }
        }

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool? Success { get; set; } = true;

        /// <summary>
        /// 信息
        /// </summary>
        public string? Msg { get; set; }

        /// <summary>
        /// 状态码
        /// </summary>
        public StatusCodeEnum? StatusCode { get; set; } = StatusCodeEnum.OK;

        /// <summary>
        /// 数据集
        /// </summary>
        public T? Data { get; set; }
    }
}
